public class ChatMoreBar extends RelativeLayout implements TextWatcher,View.OnClickListener {
	//调用此控件前首先要保证获取了如下权限
	//相机，相册，录音，读写，手机权限
	//控件内部不再判断 
	private ViewPager vpPanel;
    private View ev1,ev2,ev3,ev4;
    private List<View> myviews=new ArrayList<>();
    private ImageButton btnAdd, btnVoice, btnKeyboard, btnEmotion;
    private EditText editText;
    private boolean isSendVisible,isHoldVoiceBtn,isEmoticonReady;
    private InputMode inputMode = InputMode.NONE;
    private ChatView chatView;
    private LinearLayout morePanel,textPanel;
    private TextView voicePanel;
    private RelativeLayout emoticonPanel;
    private TextView btnSend;

	//标准构造函数
    public ChatMoreBar(Context context, AttributeSet attrs，ChatView Chatview) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.chat_input, this);
		this.chatView=chatView;
        initView();
    }

    private void initView(){
		findAllId();
        setSendBtn();
		isSendVisible = editText.getText().length() != 0;
    }
	
	//点击回调
	@Override
    public void onClick(View v) {
        Activity activity = (Activity) getContext();
        int id = v.getId();
		if(activity==null) return;
		switch(id){
			case R.id.btn_send:
				chatView.sendText();
				break;
			case R.id.btn_add:
				updateView(inputMode == InputMode.MORE ? InputMode.TEXT : InputMode.MORE);
				break;
			case R.id.btn_weizhi:
				chatView.sendLocation();
				break;
			case R.id.btn_photo:
				chatView.sendPIctuer();
				break;
			case R.id.btn_image:
				chatView.sendCamera();
				break;
			case R.id.btncard:
				chatView.senScard();
				break;
			case R.id.btn_zz:
				chatView.senMoney();
				break;
			case R.id.btn_receive:
				chatView.sendReceive();
				break;
			case R.id.btn_voice:
				updateView(InputMode.VOICE);
				break;
			case R.id.btn_keyboard:
				updateView(InputMode.TEXT);
				break;
			case R.id.btnEmoticon:
				updateView(inputMode == InputMode.EMOTICON?InputMode.TEXT:InputMode.EMOTICON);
				break;
			default:
			    break;
		}
    }
	
	//主动更新界面
    private void updateView(InputMode mode){
        if (mode == inputMode) return;
		//首先恢复默认状态
        setAllViewState();
        switch (inputMode = mode){
            case MORE:
                morePanel.setVisibility(VISIBLE);
                break;
            case TEXT:
                if (editText.requestFocus()){
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
                break;
            case VOICE:
                voicePanel.setVisibility(VISIBLE);
                textPanel.setVisibility(GONE);
                btnVoice.setVisibility(GONE);
                btnKeyboard.setVisibility(VISIBLE);
                break;
            case EMOTICON:
                if (!isEmoticonReady) {
                    prepareEmoticon();
                }
               emoticonPanel.setVisibility(VISIBLE);
                break;
        }
    }

	//重置所有view默认状态
    private void setAllViewState(){
        switch (inputMode){
            case TEXT:
                View view = ((Activity) getContext()).getCurrentFocus();
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                editText.clearFocus();
                break;
            case MORE:
                morePanel.setVisibility(GONE);
                break;
            case VOICE:
                voicePanel.setVisibility(GONE);
                textPanel.setVisibility(VISIBLE);
                btnVoice.setVisibility(VISIBLE);
                btnKeyboard.setVisibility(GONE);
                break;
            case EMOTICON:
             emoticonPanel.setVisibility(GONE);
        }
    }

	//录音状态view隐藏显示
    private void updateVoiceView(){
        if (isHoldVoiceBtn){
            voicePanel.setText(getResources().getString(R.string.chat_release_send));
            voicePanel.setBackground(getResources().getDrawable(R.drawable.btn_voice_pressed));
            chatView.startSendVoice();
        }else{
            voicePanel.setText(getResources().getString(R.string.chat_press_talk));
            voicePanel.setBackground(getResources().getDrawable(R.drawable.btn_voice_normal));
            chatView.endSendVoice();
        }
    }

	//三个textWatcher回调函数
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        isSendVisible = s!=null&&s.length()>0;
        setSendBtn();
        if (isSendVisible){
            chatView.sending();
        }
    }
    @Override
    public void afterTextChanged(Editable s) { }
	
	//重置发送按钮显示
    private void setSendBtn(){
        if (isSendVisible){
            btnAdd.setVisibility(GONE);
            btnSend.setVisibility(VISIBLE);
        }else{
            btnAdd.setVisibility(VISIBLE);
            btnSend.setVisibility(GONE);
        }
    }

	//载入表情，正式工程不这么写
    private void prepareEmoticon(){
        if (emoticonPanel == null) return;
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view1 = inflater.inflate(R.layout.emo_view, null) ;
        LinearLayout l1=(LinearLayout)view1.findViewById(R.id.my_emo) ;
        addCutEmo(l1,0);
        View view2 = inflater.inflate(R.layout.emo_view, null) ;
        LinearLayout l2=(LinearLayout)view2.findViewById(R.id.my_emo) ;
        addCutEmo(l2,3);
        View view3 = inflater.inflate(R.layout.emo_view, null) ;
        LinearLayout l3=(LinearLayout)view3.findViewById(R.id.my_emo) ;
        addCutEmo(l3,6);
        View view4 = inflater.inflate(R.layout.emo_view, null) ;
        LinearLayout l4=(LinearLayout)view4.findViewById(R.id.my_emo) ;
        addCutEmo2(l4);
        myviews.add(view1);
        myviews.add(view4);
        myviews.add(view2);
        myviews.add(view3);
        AdapterViewpager adapterViewpager=new AdapterViewpager(myviews);
        vpPanel.setAdapter(adapterViewpager);
        vpPanel.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    setDX(ev1);
                }else if(position==1){
                    setDX(ev2);
                }else if(position==2){
                    setDX(ev3);
                }else {
                    setDX(ev4);
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        isEmoticonReady = true;
    }

	//对外edittext的set get方法
    public Editable getText(){
        return editText.getText();
    }
    public void setText(String text){
        editText.setText(text);
    }
    public void setInputMode(InputMode mode){
        updateView(mode);
    }

	//自定义数据体
    public enum InputMode{TEXT,VOICE,EMOTICON,MORE,VIDEO,NONE}

	//这里只写表情页面的viewpager
	//正式工程则另写一个类
    public class AdapterViewpager extends PagerAdapter {
        private List<View> mViewList;
        public AdapterViewpager(List<View> mViewList) {
            this.mViewList = mViewList;
        }
        @Override
        public int getCount() {
            return mViewList.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return mViewList.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }
    }

	//载入静态表情，正式工程不这么写
    private void addCutEmo(LinearLayout li,int ii){
        int iii=ii+3;}
        for (int i = ii; i < iii; ++i){
            LinearLayout linearLayout = new LinearLayout(getContext());
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, dp2px(60)));
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setGravity(CENTER_VERTICAL);
            for (int j = 0;j < 7; ++j){
                try{
                    AssetManager am = getContext().getAssets();
                    final int index = 7*i+j;
                    InputStream is;
                    is = am.open(String.format("emoticon/%d.png", index));
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    Matrix matrix = new Matrix();
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    matrix.postScale(sb, sb);

                    Matrix matrix2 = new Matrix();
                    matrix2.postScale(sb3, sb3);

                    final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                    final Bitmap resizedBitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix2, true);

                    ImageView image = new ImageView(getContext());
                    image.setImageBitmap(resizedBitmap);
                    image.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
                    linearLayout.addView(image);

                    image.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String content = String.valueOf(index);
                            SpannableString str = new SpannableString(String.valueOf(index));
                            ImageSpan span = new ImageSpan(getContext(), resizedBitmap2, ImageSpan.ALIGN_BASELINE);
                            str.setSpan(span, 0, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            editText.append(str);
                        }
                    });
                    is.close();
                }catch (IOException e){
                }
            }
          li.addView(linearLayout);
        }
    }

	//载入gif表情，正式工程不这么写
    private void addCutEmo2(LinearLayout li){
        for (int i = 0; i < 2; ++i){
            LinearLayout linearLayout = new LinearLayout(getContext());
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0,1f);
            if(i==1){
                layoutParams.setMargins(0,sb777,0,sb777);
            }
            linearLayout.setLayoutParams(layoutParams);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setGravity(CENTER_VERTICAL);
            for (int j = 0;j < 4; ++j){
                final int index = 4*i+j;
                ImageView image = new ImageView(getContext());
                LinearLayout.LayoutParams ico_p=new LinearLayout.LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT,1f);
                ico_p.setMargins(sb666,0,sb666,0);
                image.setLayoutParams(ico_p);
                image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                image.setImageResource(setimg(index+""));
                linearLayout.addView(image);
                image.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        chatView.sendGif("b"+index);
                    }
                });
            }
            li.addView(linearLayout);
        }
    }

	//viewpager底部4个圆点展示设置，正式工程不这么写
    private void setDX(View someview){
        ev1.setLayoutParams(new RelativeLayout.LayoutParams(ev_yuan,ev_yuan));
        ev2.setLayoutParams(new RelativeLayout.LayoutParams(ev_yuan,ev_yuan));
        ev3.setLayoutParams(new RelativeLayout.LayoutParams(ev_yuan,ev_yuan));
        ev4.setLayoutParams(new RelativeLayout.LayoutParams(ev_yuan,ev_yuan));
        ev1.setBackgroundResource(R.drawable.yuan);
        ev2.setBackgroundResource(R.drawable.yuan);
        ev3.setBackgroundResource(R.drawable.yuan);
        ev4.setBackgroundResource(R.drawable.yuan);
        someview.setLayoutParams(new RelativeLayout.LayoutParams(ev_now,ev_now));
        someview.setBackgroundResource(R.drawable.yuan2);
    }

	//测试数据，正式工程不这么写
    private int setimg(String s){
        if(s.equals("0")){
            return R.drawable.gg00;
        }else if(s.equals("1")){
            return R.drawable.gg01;
        }else if(s.equals("2")){
            return R.drawable.gg02;
        }else if(s.equals("3")){
            return R.drawable.gg03;
        }else if(s.equals("4")){
            return R.drawable.gg04;
        }else if(s.equals("5")){
            return R.drawable.gg05;
        }else if(s.equals("6")){
            return R.drawable.gg06;
        }else if(s.equals("7")){
            return R.drawable.gg07;
        }else {
            return R.drawable.gg01;
        }
    }
	private void findAllId(){
        textPanel = (LinearLayout) findViewById(R.id.text_panel);
        btnAdd = (ImageButton) findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(this);
        btnSend = (TextView) findViewById(R.id.btn_send);
        btnSend.setOnClickListener(this);
        btnVoice = (ImageButton) findViewById(R.id.btn_voice);
        btnVoice.setOnClickListener(this);
        btnEmotion = (ImageButton) findViewById(R.id.btnEmoticon);
        btnEmotion.setOnClickListener(this);
        morePanel = (LinearLayout) findViewById(R.id.morePanel);
        LinearLayout BtnImage = (LinearLayout) findViewById(R.id.btn_photo);
        BtnImage.setOnClickListener(this);
        LinearLayout BtnPhoto = (LinearLayout) findViewById(R.id.btn_image);
        BtnPhoto.setOnClickListener(this);
        LinearLayout btnVideo = (LinearLayout) findViewById(R.id.btn_video);
        btnVideo.setOnClickListener(this);
        LinearLayout btnFile = (LinearLayout) findViewById(R.id.btn_file);
        btnFile.setOnClickListener(this);
        LinearLayout btnZz = (LinearLayout) findViewById(R.id.btn_zz);
        btnZz.setOnClickListener(this);
        LinearLayout btnSc= (LinearLayout) findViewById(R.id.send_shoucang);
        btnSc.setOnClickListener(this);
        LinearLayout btnMp= (LinearLayout) findViewById(R.id.btn_mp);
        btnMp.setOnClickListener(this);
        LinearLayout btn_weizhi = (LinearLayout) findViewById(R.id.btn_weizhi);
        btn_weizhi.setOnClickListener(this);
        btnKeyboard = (ImageButton) findViewById(R.id.btn_keyboard);
        btnKeyboard.setOnClickListener(this);
        voicePanel = (TextView) findViewById(R.id.voice_panel);
        editText = (EditText) findViewById(R.id.input);
        emoticonPanel = (RelativeLayout) findViewById(R.id.emoticonPanel);
        vpPanel=(ViewPager)findViewById(R.id.chat_vp);
        ev1=(View)findViewById(R.id.emov1);
        ev2=(View)findViewById(R.id.emov2);
        ev3=(View)findViewById(R.id.emov3);
        ev4=(View)findViewById(R.id.emov4);
		voicePanel.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction()) {
						case MotionEvent.ACTION_DOWN:
							isHoldVoiceBtn = true;
							updateVoiceView();
							break;
						case MotionEvent.ACTION_UP:
							isHoldVoiceBtn = false;
							updateVoiceView();
							break;
					}
					return true;
				}
			});
        editText.addTextChangedListener(this);
        editText.setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (hasFocus) {
						updateView(InputMode.TEXT);
					}
				}
			});
	}
}
